<script setup>
import { onMounted, ref, reactive } from 'vue';
import { saveSessionApi, getSessionListApi, updateSessionApi, getSessionInfoApi } from'@/apis/sms/session'

const params = reactive({
  name: '',
  status: '',
  pageNum:1,
  pageSize:10,
})

// 获取数据
const sessionData = ref({})
const getSessionListData = async() => {
  const res = await getSessionListApi(params)
  sessionData.value = res.data
}

// 搜索
const handleSearch = () => {
  getSessionListData()
}

// 重置
const handleRefresh = () => {
  params.name = '';
  params.status = '';
  params.pageNum = 1,
  params.pageSize = 10,
  getSessionListData()
}

// 展示面板,标题
const dialogVisible = ref(false)
const title = ref('')

// 数据
const sessionForm = ref({})
// 添加广告/修改
const handleAddOrUpdate = async(id) => {
  sessionForm.value = {}
  if(id !== null){
    const res = await getSessionInfoApi(id)
    sessionForm.value = res.data
  }
  dialogVisible.value = true;
}


// 确定
const handleConfirm = () => {
  // 没有id是添加
  if (sessionForm.value.id === null || sessionForm.value.id === '' || sessionForm.value.id === void 0) {
    console.log(JSON.stringify(sessionForm.value))
    saveSessionApi(sessionForm.value)
    .then(res => {
      if(res.code === 0){
        ElMessage({
            message: '添加成功',
            type: 'success',
        })
        getSessionListData()
        dialogVisible.value = false;
      }
    })
  } else {
    // 有是修改
    updateSessionApi(sessionForm.value)
    .then(res => {
      if(res.code === 0){
        ElMessage({
            message: '修改成功',
            type: 'success',
        })
        getSessionListData()
        dialogVisible.value = false;
      }
    })
  }
}

onMounted(() => {
  getSessionListData()
})
</script>

<template>
  <div class="container">
    <el-row>
      <el-col :span="24" style="display: flex;justify-content: flex-start;align-items: center;margin-bottom: 10px;">
        <span style="display: inline-block;width: 70px;">场次名称</span>
        <el-input v-model="params.name" placeholder="Please input" clearable style="width: 280px;" />
        <span style="display: inline-block;width: 70px;margin-left: 10px;">场次状态</span>
        <el-select v-model="params.status" clearable placeholder="Select" style="width: 280px;">
          <el-option :key="0" label="上线" :value="0" />
          <el-option :key="1" label="下线" :value="1" />
        </el-select>
        <el-button type="primary" :icon="Search" style="margin-left: 10px;" @click="handleSearch">搜索</el-button>
        <el-button @click="handleRefresh">重置</el-button>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="24" style="display: flex;justify-content: flex-start;align-items: center;margin-bottom: 10px;">
        <el-button type="primary" plain @click="handleAddOrUpdate(null)">添加场次</el-button>
        <el-button type="danger" plain @click="handleRemoves">批量删除</el-button>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="24">
        <el-table :data="sessionData.list" style="width: 100%" row-key="id" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" />
          <el-table-column prop="name" label="活动名称" width="150" />
          <el-table-column prop="startTime" label="每日开始时间" align="center"/>
          <el-table-column prop="endTime" label="每日结束时间" align="center"/>
          <el-table-column prop="status" label="状态" align="center">
            <template v-slot="scoped">
              <el-switch v-model="scoped.row.status" class="ml-2"
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949" :active-value="0"
                :inactive-value="1" @change="updateStatus(scoped.row)" />
            </template>
          </el-table-column>
          <el-table-column label="操作" width="240" fixed="right" align="center">
            <template v-slot="scoped">
              <el-button type="primary" text size="small" :icon="EditPen"
                @click="handleAddOrUpdate(scoped.row.id)">修改</el-button>
              <el-button type="primary" text size="small" :icon="Delete"
                @click="handleRemove(scoped.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
  </div>

  <el-dialog v-model="dialogVisible" :title="title" style="width: 55%;">
    <el-form style="width: 96%;" :model="sessionForm" :rules="rules" status-icon label-width="120px">
      <el-form-item label="场次名称" prop="title">
        <el-input v-model="sessionForm.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="每日开始时间" prop="startTime">
        <el-time-picker
          v-model="sessionForm.startTime"
          value-format="HH:mm:ss"
          placeholder="请选择开始时间"
        />
      </el-form-item>
      <el-form-item label="每日结束时间" prop="endTime">
        <el-time-picker
          v-model="sessionForm.endTime"
          value-format="HH:mm:ss"
          placeholder="请选择结束时间"
        />
      </el-form-item>
      <el-form-item label="是否启用" prop="visible">
        <el-switch v-model="sessionForm.status" class="ml-2"
          style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949" :active-value="0" :inactive-value="1" />
      </el-form-item>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style scoped lang='scss'>

</style>